package com.xspace.android.imcommon.interfaces;

import android.os.RemoteException;

import com.xspace.android.imcommon.entity.ChatMsg;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class MessageMg implements IToChatListener{
	private ServiceManager mServiceManager = null;
	private Map<String, IToChatListener> mToChatListenerMap = new HashMap<String, IToChatListener>();
	private MessageMg() {
		mServiceManager = ImServiceConnection.getInstance().getImServiceConnection();
	}

	private static MessageMg instance = null;

	public static MessageMg getInstance() {
		if (null == instance) {
			instance = new MessageMg();
		}
		return instance;
	}

	/**
	 * 用户列表模块注册
	 */
	public void initImUser(String key,String userId, final IToUserListener callBack, final int pageSize) {
		try {
			mServiceManager.initImUser(key,userId, callBack, pageSize);
		} catch (Exception e) {
		}
	}
	public void removeIToUserListener(String key){
		try {
			mServiceManager.removeIToUserListener(key);
		} catch (Exception e) {
		}
	}

	/**
	 * 获取用户列表
	 * 
	 */
	public void queryUserList() {
		try {
			mServiceManager.queryUserList();
		} catch (Exception e) {
		}
	}

	/**
	 * 修改用户信息
	 */
	public void updateUserInfo(String userId, String userName, String userHead, int whiteStatus) {
		try {
			mServiceManager.updateUserInfo(userId, userName, userHead, whiteStatus);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 退出用户列表页面时调用
	 */
	public void unImUser() {
		try {
			mServiceManager.unImUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除对话
	 */
	public void deleteImUser(String userId) {
		try {
			mServiceManager.deleteImUser(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除消息
	 */
	public boolean deleteMessage(int msgId) {
		try {
			return mServiceManager.deleteMsg(msgId);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 即时聊天模块注册
	 * 
	 * @param callBack
	 */
	public void initSendMg(IToMsgListener callBack, String userName, String toUser) {
		try {
			mServiceManager.initSendMessage(callBack, userName, toUser);
		} catch (RemoteException e) {
		}
	}
    public  void initChatInfo(String ClassName ,IToChatListener  mcallback) {
    	if (mToChatListenerMap != null) {
    		mToChatListenerMap.put(ClassName, mcallback);
    	}
    	try {
			mServiceManager.initChatInfoMessage(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}
    public void removeChatInfoListener(String ClassName) {
    	if (mToChatListenerMap != null) 
		mToChatListenerMap.remove(ClassName);
}
	/**
	 * 即时聊天模块消息发送
	 * 
	 * @param callBack
	 */
	public void sendMg(ChatMsg req) {
		ChatAidl info = new ChatAidl(req);
		try {
			mServiceManager.sendMg(info);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 即时聊天模块消息查询
	 * 
	 * @param callBack
	 */
	public void queryMsgList(int startRecord, int pageSize) {
		try {
			mServiceManager.queryMsgList(startRecord, pageSize);
		} catch (RemoteException e) {
		}
	}

	/**
	 * 即时聊天模块历史消息查询
	 * 
	 * @param callBack
	 */
	public void queryHistoryMsgList() {
		try {
			mServiceManager.queryHistoryMsgList();
		} catch (RemoteException e) {
		}
	}

	/**
	 * 退出即时聊天页面时调用，重置当前聊天对象
	 * 
	 * @param callBack
	 */
	public void unSendMessage() {
		try {
			mServiceManager.unSendMessage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onToNewMsg(ChatMsg info) {
		Iterator<Entry<String, IToChatListener>> iter = mToChatListenerMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, IToChatListener> entry = iter.next();
			Object key = entry.getKey();
			IToChatListener mListener = mToChatListenerMap.get(key);
			mListener.onToNewMsg(info);
		}
		
	}

}